package at.guthy;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;

import com.google.gdata.util.AuthenticationException;

public class PicasaWindow extends JFrame {
	public static JProgressBar progressBar = new JProgressBar();
	public static JTextField usernameField = new JTextField();
	public static JPasswordField passwordField = new JPasswordField();
	public static PicasaPicturesTable picturesTable = new PicasaPicturesTable();
	public static PicasaAlbumSelector albumSelector = PicasaAlbumSelector.getInstance();

	public PicasaWindow() {
		this.setLayout(new BorderLayout());
		this.add(new PicasaCredentialsFrame().getContentPane(), BorderLayout.NORTH);
		this.add(new PicasaSplitPane(), BorderLayout.CENTER);
	}
}

class PicasaSplitPane extends JSplitPane {
	public PicasaSplitPane() {

		this.setLayout(new BorderLayout());
		this.add(PicasaWindow.albumSelector, BorderLayout.NORTH);
		this.add(new JScrollPane(PicasaWindow.picturesTable), BorderLayout.CENTER);
		this.add(PicasaWindow.progressBar, BorderLayout.SOUTH);
	}
}

class PicasaCredentialsFrame extends JFrame {
	public PicasaCredentialsFrame() {
		super();
		this.setLayout(new GridLayout(2, 3));
		PicasaWindow.usernameField.setMinimumSize(new Dimension(60, 25));
		this.add(new JLabel("E-Mail"));
		this.add(PicasaWindow.usernameField);
		JButton loginButton = new JButton("Login");
		loginButton.addActionListener(new LoginButtonListener());
		this.add(loginButton);
		this.add(new JLabel("Password"));
		this.add(PicasaWindow.passwordField);
		this.setVisible(true);
	}
}

class LoginButtonListener implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent arg0) {
		System.out.println("AL");
		try {
			PicasaClient.getInstance().login();
			PicasaAlbumSelector.getInstance().refresh();
		}
		catch (AuthenticationException e) {
			e.printStackTrace();
			PicasaWindow.progressBar.setString("Login unsuccessful");
		}
	}
	
}